//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import sun.misc.SharedSecrets;

public class SealedObject implements Serializable {
    static final long serialVersionUID = 4482838265551344752L;
    private byte[] encryptedContent = null;
    private String sealAlg = null;
    private String paramsAlg = null;
    protected byte[] encodedParams = null;

    public SealedObject(Serializable var1, javax.vpncrypto.Cipher var2) throws IOException, javax.vpncrypto.IllegalBlockSizeException {
        ByteArrayOutputStream var3 = new ByteArrayOutputStream();
        ObjectOutputStream var4 = new ObjectOutputStream(var3);

        byte[] var5;
        try {
            var4.writeObject(var1);
            var4.flush();
            var5 = var3.toByteArray();
        } finally {
            var4.close();
        }

        try {
            this.encryptedContent = var2.doFinal(var5);
        } catch (javax.vpncrypto.BadPaddingException var9) {
        }

        if (var2.getParameters() != null) {
            this.encodedParams = var2.getParameters().getEncoded();
            this.paramsAlg = var2.getParameters().getAlgorithm();
        }

        this.sealAlg = var2.getAlgorithm();
    }

    protected SealedObject(SealedObject var1) {
        this.encryptedContent = (byte[])var1.encryptedContent.clone();
        this.sealAlg = var1.sealAlg;
        this.paramsAlg = var1.paramsAlg;
        if (var1.encodedParams != null) {
            this.encodedParams = (byte[])var1.encodedParams.clone();
        } else {
            this.encodedParams = null;
        }

    }

    public final String getAlgorithm() {
        return this.sealAlg;
    }

    public final Object getObject(Key var1) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyException {
        if (var1 == null) {
            throw new NullPointerException("key is null");
        } else {
            try {
                return this.unseal(var1, (String)null);
            } catch (NoSuchProviderException var3) {
                throw new NoSuchAlgorithmException("algorithm not found");
            } catch (javax.vpncrypto.IllegalBlockSizeException var4) {
                throw new InvalidKeyException(var4.getMessage());
            } catch (javax.vpncrypto.BadPaddingException var5) {
                throw new InvalidKeyException(var5.getMessage());
            }
        }
    }

    public final Object getObject(javax.vpncrypto.Cipher var1) throws IOException, ClassNotFoundException, javax.vpncrypto.IllegalBlockSizeException, javax.vpncrypto.BadPaddingException {
        ObjectInputStream var2 = this.getExtObjectInputStream(var1);

        Object var4;
        try {
            Object var3 = var2.readObject();
            var4 = var3;
        } finally {
            var2.close();
        }

        return var4;
    }

    public final Object getObject(Key var1, String var2) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException {
        if (var1 == null) {
            throw new NullPointerException("key is null");
        } else if (var2 != null && var2.length() != 0) {
            try {
                return this.unseal(var1, var2);
            } catch (javax.vpncrypto.BadPaddingException | javax.vpncrypto.IllegalBlockSizeException var4) {
                throw new InvalidKeyException(var4.getMessage());
            }
        } else {
            throw new IllegalArgumentException("missing provider");
        }
    }

    private Object unseal(Key var1, String var2) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, javax.vpncrypto.IllegalBlockSizeException, javax.vpncrypto.BadPaddingException {
        AlgorithmParameters var3 = null;
        if (this.encodedParams != null) {
            try {
                if (var2 != null) {
                    var3 = AlgorithmParameters.getInstance(this.paramsAlg, var2);
                } else {
                    var3 = AlgorithmParameters.getInstance(this.paramsAlg);
                }
            } catch (NoSuchProviderException var18) {
                if (var2 == null) {
                    throw new NoSuchAlgorithmException(this.paramsAlg + " not found");
                }

                throw new NoSuchProviderException(var18.getMessage());
            }

            var3.init(this.encodedParams);
        }

        javax.vpncrypto.Cipher var4;
        try {
            if (var2 != null) {
                var4 = javax.vpncrypto.Cipher.getInstance(this.sealAlg, var2);
            } else {
                var4 = javax.vpncrypto.Cipher.getInstance(this.sealAlg);
            }
        } catch (NoSuchPaddingException var16) {
            throw new NoSuchAlgorithmException("Padding that was used in sealing operation not available");
        } catch (Exception var17) {
            if (var2 == null) {
                throw new NoSuchAlgorithmException(this.sealAlg + " not found");
            }

            throw new NoSuchProviderException(var17.getMessage());
        }

        try {
            if (var3 != null) {
                var4.init(2, var1, var3);
            } else {
                var4.init(2, var1);
            }
        } catch (InvalidAlgorithmParameterException var15) {
            throw new RuntimeException(var15.getMessage());
        }

        ObjectInputStream var5 = this.getExtObjectInputStream(var4);

        Object var7;
        try {
            Object var6 = var5.readObject();
            var7 = var6;
        } finally {
            var5.close();
        }

        return var7;
    }

    private void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
        var1.defaultReadObject();
        if (this.encryptedContent != null) {
            this.encryptedContent = (byte[])this.encryptedContent.clone();
        }

        if (this.encodedParams != null) {
            this.encodedParams = (byte[])this.encodedParams.clone();
        }

    }

    public ObjectInputStream getExtObjectInputStream(Cipher var1) throws BadPaddingException, IllegalBlockSizeException, IOException {
        byte[] var2 = var1.doFinal(this.encryptedContent);
        ByteArrayInputStream var3 = new ByteArrayInputStream(var2);
        return new javax.vpncrypto.extObjectInputStream(var3);
    }

    // todo: 这里我改了哦！！！
    static {
        SharedSecrets.setJavaxCryptoSealedObjectAccess((var0, var1) -> {
//            return var0.getExtObjectInputStream(var1);
            return null;
        });
    }
}
